<template>
	<view class="form-main">
		<u-form :model="form" ref="uForm" :error-type="['message']">
			<view class="form-column">
				<view class="form-title">筛选条件</view>
				<view class="form-box">
					<u-form-item label="院区" label-width="80rpx" prop="deptName">
						<view class="u-flex select-box">
							<u-input class="u-flex-1 form-input" type="select" :select-open="yqShow" @click="yqShow = true" placeholder="请选择院区，不选则查全部" v-model="form.deptName" />
						</view>
						<u-select v-model="yqShow" mode="single-column" :list="yqList" value-name="id" label-name="name" @confirm="onConfirmType1"></u-select>
					</u-form-item>
					<u-form-item label="开始日期" label-width="140rpx" prop="dateStar">
						<view class="u-flex select-box">
							<u-input class="u-flex-1 form-input" type="select" :select-open="dateStarShow" @click="dateStarShow = true" placeholder="请选择开始日期" v-model="form.dateStar" />
						</view>
						<u-picker v-model="dateStarShow" mode="time" @confirm="onConfirmType2"></u-picker>
					</u-form-item>
					<u-form-item label="结束日期" label-width="140rpx" prop="dateEnd">
						<view class="u-flex select-box">
							<u-input class="u-flex-1 form-input" type="select" :select-open="dateEndShow" @click="dateEndShow = true" placeholder="请选择结束日期" v-model="form.dateEnd" />
						</view>
						<u-picker v-model="dateEndShow" mode="time" @confirm="onConfirmType3"></u-picker>
					</u-form-item>
					<u-form-item label="主刀医生" label-width="140rpx" prop="zdYs">
						<u-input class="form-input" v-model="form.zdYs" placeholder="主刀医生" />
					</u-form-item>
				</view>
			</view>
		</u-form>
		<view class="u-margin-top-30 flex justify-end">
			<view @click="exportExcel">
				<animate-button ref="animeBtn1" btn-text="导出"></animate-button>
			</view>
			<view class="u-margin-left-10 u-margin-right-10"></view>
			<view @click="lookList">
				<animate-button ref="animeBtn2" btn-text="查看"></animate-button>
			</view>
		</view>
		<u-toast ref="uToast"></u-toast>
	</view>
</template>

<script>
	import { baseURL } from '@/utils/request/index.js'
	export default {
		data() {
			return {
				yqShow: false,
				dateStarShow: false,
				dateEndShow: false,
				yqList: [],
				form: {
					deptId: '',
					deptName: '',
					dateStar: '',
					dateEnd: '',
					zdYs: ''
				},
			};
		},
		onLoad() {
			this.getDepList();
		},
		methods: {
			getDepList() {
				this.$api.deptList({
					current: 1,
					size: 999
				}).then(res => {
					let data = res.data
					for (let i of data.records) {
						this.yqList.push({
							name: i.deptName,
							id: i.deptId
						})
					}
				})
			},
			onConfirmType1(item) {
				this.form.deptId = item[0].value
				this.form.deptName = item[0].label
			},
			onConfirmType2(item) {
				this.form.dateStar = `${item.year}-${item.month}-${item.day}`
			},
			onConfirmType3(item) {
				this.form.dateEnd = `${item.year}-${item.month}-${item.day}`
			},
			lookList() {
				this.$refs.uForm.validate(valid => {
					if (valid) {
						this.$Router.push({ name: 'followUp_list', params: this.form })
					} else {
						return
					}
				});
			},
			exportExcel() {
				const token = uni.getStorageSync('tokenInfo');
				// uni.downloadFile({
				// 	url: baseURL + `y/hf/param/export?deptId=${this.form.deptId}&dateParam=${this.form.dateParam}`,
				// 	header: {
				// 		[token.tokenName]: token.tokenValue
				// 	},
				// 	success(res) {
				// 		uni.openDocument({
				// 			showMenu: true,
				// 			fileType: 'xlsx',
				// 			filePath: res.tempFilePath,
				// 			success: function(res) {}
				// 		});
				// 	}
				// })
				// #ifdef H5
					uni.request({
						url: baseURL + `y/hf/param/export?deptId=${this.form.deptId}&dateStar=${this.form.dateStar}&dateEnd=${this.form.dateEnd}&zdYs=${this.form.zdYs}`,
						responseType: 'arraybuffer',
						header: {
							[token.tokenName]: token.tokenValue
						},
						success: (response) => {
							let pdfData = response.data;
							let blob = new Blob([ pdfData], {
							  type: 'application/xlsx;charset=UTF-8'
							})
							var a = document.createElement('a');
							document.body.appendChild(a);
							a.style = 'display: none';
							var url = window.URL.createObjectURL(blob);
							a.href = url;
							a.download = `回访数据表 - ${this.$u.timeFormat(new Date(), 'yyyy年mm月dd日 hh时MM分')}.xlsx`;
							a.click();
							a.remove();
							window.URL.revokeObjectURL(url);  
						},
						fail: err => {
							 console.log(err)
						}
					});
				// #endif
			}
		},
	}
</script>

<style>
	.select-box{
		width: 100%;
	}
</style>